package com.ruoyi.youtian.service.impl;

import java.util.List;
import com.ruoyi.youtian.domain.PointAttribute;
import com.ruoyi.youtian.domain.PointEquipment;
import com.ruoyi.youtian.domain.ScenePoint;
import com.ruoyi.youtian.domain.vo.ScenePointVo;
import com.ruoyi.youtian.mapper.PointAttributeMapper;
import com.ruoyi.youtian.mapper.PointEquipmentMapper;
import com.ruoyi.youtian.mapper.ScenePointMapper;
import com.ruoyi.youtian.service.IScenePointService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 场景与点位关联Service业务层处理
 *
 * @author ruoyi
 * @date 2022-09-15
 */
@Service
public class ScenePointServiceImpl implements IScenePointService
{
    @Autowired
    private ScenePointMapper scenePointMapper;

    @Autowired
    private PointAttributeMapper pointAttributeMapper;

    @Autowired
    private PointEquipmentMapper pointEquipmentMapper;

    /**
     * 查询场景与点位关联
     *
     * @param id 场景与点位关联主键
     * @return 场景与点位关联
     */
    @Override
    public ScenePoint selectScenePointById(Long id)
    {
        return scenePointMapper.selectScenePointById(id);
    }

    /**
     * 查询场景与点位关联列表
     *
     * @param scenePoint 场景与点位关联
     * @return 场景与点位关联
     */
    @Override
    public List<ScenePoint> selectScenePointList(ScenePoint scenePoint)
    {
        return scenePointMapper.selectScenePointList(scenePoint);
    }

    /**
     * 新增场景与点位关联
     *
     * @param scenePointVo 场景与点位关联
     * @return 结果
     */
    @Override
    public int insertScenePoint(ScenePointVo scenePointVo)
    {
        PointAttribute pointAttribute = new PointAttribute();
        pointAttribute.setSceneId(scenePointVo.getSceneId());
        pointAttribute.setPointId(scenePointVo.getPointId());
        pointAttribute.setUpperLimit(scenePointVo.getUpperLimit());
        pointAttribute.setLowerLimit(scenePointVo.getLowerLimit());
        pointAttributeMapper.insertPointAttribute(pointAttribute);
        ScenePoint scenePoint = new ScenePoint();
        scenePoint.setPointId(scenePointVo.getPointId());
        scenePoint.setSceneId(scenePointVo.getSceneId());
        scenePoint.setCreateBy(scenePointVo.getCreateBy());
        return scenePointMapper.insertScenePoint(scenePoint);
    }

    /**
     * 修改场景与点位关联
     *
     * @param scenePoint 场景与点位关联
     * @return 结果
     */
    @Override
    public int updateScenePoint(ScenePoint scenePoint)
    {
        return scenePointMapper.updateScenePoint(scenePoint);
    }

    /**
     * 批量删除场景与点位关联
     *
     * @param ids 需要删除的场景与点位关联主键
     * @return 结果
     */
    @Override
    public int deleteScenePointByIds(Long[] ids)
    {
        for (Long id : ids) {
            ScenePoint scenePoint = scenePointMapper.selectScenePointById(id);
            PointAttribute pointAttribute = new PointAttribute();
            pointAttribute.setSceneId(scenePoint.getSceneId());
            pointAttribute.setPointId(pointAttribute.getPointId());
            pointAttributeMapper.deletePointAttributeByPointAttribute(pointAttribute);
        }
        return scenePointMapper.deleteScenePointByIds(ids);
    }

    /**
     * 删除场景与点位关联信息
     *
     * @param id 场景与点位关联主键
     * @return 结果
     */
    @Override
    public int deleteScenePointById(Long id)
    {
        return scenePointMapper.deleteScenePointById(id);
    }

    @Override
    public int scenePointRemove(ScenePoint scenePoint) {
        // pointId
        PointAttribute pointAttribute = new PointAttribute();
        pointAttribute.setSceneId(scenePoint.getSceneId()); // 删除默认采集点位上下限值
        pointAttribute.setPointId(scenePoint.getPointId());
        pointAttribute.setUpdateBy(scenePoint.getUpdateBy());
        pointAttributeMapper.deletePointAttributeByPointAttribute(pointAttribute);
        return scenePointMapper.scenePointRemove(scenePoint);
    }
}
